/**
 * Copyright (c) 2010 by Kevin Bierhoff.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the authors nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.syper.javasyp.formulae;

import org.eclipse.jdt.core.dom.InfixExpression.Operator;

/**
 * @author kevin
 * @since Jan 30, 2010
 *
 */
public class Infix implements Formula {

	public static Infix equals(Formula f1, Formula f2) {
		return new Infix(f1, Operator.EQUALS, f2);
	}

	public static Infix and(Formula f1, Formula f2) {
		return new Infix(f1, Operator.AND, f2);
	}

	public static Infix or(Formula f1, Formula f2) {
		return new Infix(f1, Operator.OR, f2);
	}

	public static Infix implies(Formula antecedent, Formula consequence) {
		return or(Prefix.not(antecedent), consequence);
	}

	/**
	 * Concatenates the given operands with the given operator.
	 * Throws if called with empty list.
	 * @param op operator to use
	 * @param operands
	 * @return the only operand or a chain of {@link Infix} formulae.
	 */
	public static Formula toInfix(Operator op, Iterable<Formula> operands) {
		Formula result = null;
		for (Formula f : operands) {
			if (result == null)
				result = f;
			else
				result = new Infix(result, op, f);
		}
		if (result == null)
			throw new IllegalArgumentException("empty operand list");
		return result;
	}

	/**
	 * Concatenates the given operands with the given operator.
	 * Throws if called with empty list.
	 * @param op operator to use
	 * @param operands
	 * @return the only operand or a chain of {@link Infix} formulae.
	 */
	public static Formula toInfix(Operator op, Formula... operands) {
		Formula result = null;
		for (Formula f : operands) {
			if (result == null)
				result = f;
			else
				result = new Infix(result, op, f);
		}
		if (result == null)
			throw new IllegalArgumentException("empty operand list");
		return result;
	}

	/**
	 * Use this to e.g. create a list of post-conditions or invariants.
	 * Concatenates the given operands with the given operator into a
	 * synthetic formula.
	 * Throws if called with empty list.
	 * @param op operator to use
	 * @param operands
	 * @return the only operand or a chain of synthetic {@link Infix} formulae.
	 */
	public static Formula toSynthetic(Operator op, Formula... operands) {
		Formula result = null;
		for (Formula f : operands) {
			if (result == null)
				result = f;
			else
				result = new Infix(result, op, f, true);
		}
		if (result == null)
			throw new IllegalArgumentException("empty operand list");
		return result;
	}

	private final Formula lhs;
	private final Formula rhs;
	private final Operator op;
	/** Best-effort; {@code true} if this formula encodes e.g. a post-condition list */
	private final boolean isSynthetic;

	/**
	 * Use & (respectively |) for logical and (respectively or)
	 * @param lhs
	 * @param rhs
	 * @param op
	 */
	public Infix(Formula lhs, Operator op, Formula rhs) {
		this(lhs, op, rhs, false);
	}

	/**
	 * Use & (respectively |) for logical and (respectively or)
	 * @param lhs
	 * @param rhs
	 * @param op
	 * @param isSynthetic use {@code true} if this formula encodes e.g.
	 * a list of post-conditions.
	 */
	private Infix(Formula lhs, Operator op, Formula rhs, boolean isSynthetic) {
		super();
		this.lhs = lhs;
		this.rhs = rhs;
		this.op = op;
		this.isSynthetic = isSynthetic;
	}

	public Formula getLhs() {
		return lhs;
	}

	public Formula getRhs() {
		return rhs;
	}

	public Operator getOp() {
		return op;
	}

	public boolean isSynthetic() {
		return isSynthetic;
	}

	@Override
	public <T> T dispatch(FormulaVisitor<T> visitor) {
		return visitor.visit(this);
	}

	@Override
	public boolean isBoolean() {
		if (lhs.isBoolean() && rhs.isBoolean())
			return true;
		if (Operator.EQUALS.equals(op) ||
				Operator.NOT_EQUALS.equals(op) ||
				Operator.GREATER.equals(op) ||
				Operator.GREATER_EQUALS.equals(op) ||
				Operator.LESS.equals(op) ||
				Operator.LESS_EQUALS.equals(op) ||
				Operator.CONDITIONAL_AND.equals(op) ||
				Operator.CONDITIONAL_OR.equals(op))
			return true;
		return false;
	}

	@Override
	public String toString() {
		return lhs + (isSynthetic ? " _" : " ") + op + " " + rhs;
	}
}
